﻿using IService.Managemen;
using Microsoft.AspNetCore.Mvc.Rendering;
using Models.Managemen;
using ModelToDto;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace Service.Managemen
{
    public class MenueService : BaseService, IMenueService
    {
        public ISqlSugarClient _sqlSugarClient { get; set; }
        public MenueService(ISqlSugarClient _sqlSugarClient) : base(_sqlSugarClient)
        {
            this._sqlSugarClient = _sqlSugarClient;
        }
        public List<Mp_Menue> GetMenueList(Expression<Func<Mp_Menue, bool>> expression, int leveL = 0)
        {
            var tree = sqlSugarClient.Queryable<Mp_Menue>().Where(expression).ToTree(it => it.ChildList, it => it.ParentId, leveL);
            return tree;
        }
        /// <summary>
        /// 添加页面按钮管理
        /// </summary>
        /// <param name="mp_Button"></param>
        /// <returns></returns>
        public async Task<bool> AddMuneBtn(Mp_Button mp_Button)
        {
            var exble = Expressionable.Create<Mp_Button>();
            exble.And(t => t.BtnName == mp_Button.BtnName);
            var selBtn = await FindQueryForWhere(exble.ToExpression());
            if (selBtn != null)
            {
                return false;
            }
            var instEnt = Insert(mp_Button);
            if (instEnt > 0)
            {
                return true;
            }
            return false;
        }
        /// <summary>
        /// 给菜单分配按钮功能
        /// </summary>
        /// <param name="muneId"></param>
        /// <param name="btnId"></param>
        /// <returns></returns>
        public async Task<bool> AssigMuneBtn(int muneId, List<int> btnId)
        {
            try
            {
                string strBtnIds = string.Join(", ", btnId);
                var selMenu = IFindEntityForID<Mp_Menue>(muneId);
                selMenu.MeuneButton = strBtnIds;
                Update(selMenu);
            }
            catch (Exception)
            {

                return false;
            }
            return true;
        }
        /// <summary>
        /// 获取相关菜单里选择和未选择的按钮
        /// </summary>
        /// <param name="muneId"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<List<SelectListItem>> GetAllMunBtn(int muneId)
        {
            var menuBtn = IFindEntityForID<Mp_Menue>(muneId).MeuneButton?.Split(',');
            var btnAll = await FindQueryForWhere<Mp_Button>(t => true);

            List<SelectListItem> selectListItems = new List<SelectListItem>();
            foreach (var item in btnAll)
            {
                SelectListItem selectListItem = new SelectListItem()
                {
                    Selected = menuBtn.Contains(item.BtnId.ToString()),
                    Text = item.BtnName.ToString(),
                    Value = item.BtnId.ToString()
                };
                selectListItems.Add(selectListItem);
            }
            return selectListItems;
        }
    }
}
